import { Toast } from 'vant'
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import basePage from '@/components/basePage.vue'
// import Home from '../views/Home.vue'
// import treat_record from '@/views/home/treat_record.vue'
// import medical_record from '@/views/home/medical_record.vue'
import patientInfo from '@/views/patient/patientInfo.vue'
import add_patient from '@/views/patient/add_patient.vue'
import medical_record_list from '@/views/medical_record/medical_record_list.vue'
import medical_record_details from '@/views/medical_record/medical_record_details.vue'
import add_medical_record from '@/views/medical_record/add_medical_record.vue'
import treat_record_list from '@/views/treat_record/treat_record_list.vue'
import treat_record_details from '@/views/treat_record/treat_record_details.vue'
import add_treat_record from '@/views/treat_record/add_treat_record.vue'
import register from '@/views/home/register.vue'
import login from '@/views/home/login.vue'
import user_info from '@/views/user/user_info.vue'
import drugs_list from '@/views/drugs/drugs_list.vue'
import drugs_details from '@/views/drugs/drugs_details.vue'
import add_drugs from '@/views/drugs/add_drugs.vue'

import test from '@/views/test.vue'



const routes = [
  {
    path: '/',
    name: 'Home',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      // { path:'/',redirect:'/patientInfo' },
      { path:'/',redirect:'/medical_record_list' },
      { path:'/add_patient',name: 'add_patient',components:{ content:add_patient } },
      { path:'/patientInfo',name: 'patientInfo',components:{ content:patientInfo } },
      // { path:'/treat_record',name: 'treat_record',components:{ content:treat_record } },
      // { path:'/medical_record',name: 'medical_record',components:{ content:medical_record } },
      
    ]
  },
  {
    path:'/register',
    name:'register',
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:register } },
    ]
  },
  {
    path:'/login',
    name:'login',
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:login } },
    ]
  },
  {
    path:'/medical_record_list',
    name:'medical_record_list',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:medical_record_list } },
    ]
  },
  {
    // path:'/medical_record_details/:id',
    path:'/medical_record_details',
    name:'medical_record_details',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:medical_record_details } },
    ]
  },
  {
    path:'/add_medical_record',
    name:'add_medical_record',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:add_medical_record } },
    ]
  },
  {
    path:'/treat_record_list',
    name:'treat_record_list',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:treat_record_list } },
    ]
  },
  
  {
    path:'/treat_record_details',
    name:'treat_record_details',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:treat_record_details } },
    ]
  },
  {
    path:'/add_treat_record',
    name:'add_treat_record',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:add_treat_record } },
    ]
  },
  {
    path:'/user_info',
    name:'user_info',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:user_info } },
    ]
  },
  {
    path:'/drugs_list',
    name:'drugs_list',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:drugs_list } },
    ]
  },
  {
    // path:'/drugs_details/:id',
    path:'/drugs_details',
    name:'drugs_details',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:drugs_details } },
    ]
  },
  {
    path:'/add_drugs',
    name:'add_drugs',
    meta:{is_login:true},  // 如果要访问的页面需要登录了才能访问，就加上这个属性
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:add_drugs } },
    ]
  },
  
  // {
  //   path:'/add_patient',
  //   name:'add_patient',
  //   component:add_patient
  // },
  {
    path:'/test',
    name:'test',
    component: basePage,
    children:[
      { path:'',name: '',components:{ default:basePage, content:test } },
    ]
  },
]

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to,from,next)=>{
  // 路由跳转前执行的操作
  const logininfo = JSON.parse(localStorage.getItem("logininfo"));  // 把字符串转json格式
  // if(!logininfo && to.meta.is_login==true){
  if(!logininfo && to.meta.is_login==true){
    Toast.fail("你还未登录 请先登录")  // 由于这里还访问不了vue对象，所以vant的 toast轻提示 得这样用
    setTimeout(()=>{
      router.push('/login');
      
    })
    return
  }
  next()
})

export default router
